<template>
    <li class="Purchase">
        <el-form ref="form" :model="form" label-width="68px">
            <el-form-item :label="$t('jiaoyi[0]')">
                <el-input v-model="form.buy_price" :placeholder="$t('jiaoyi[1]')"></el-input>
            </el-form-item>
            <el-form-item :label="$t('jiaoyi[2]')">
                <el-input v-model="form.buy_num"></el-input>
                <el-radio-group v-model="radio1" @change="buynumchange">
                    <el-radio-button label="0.25">25%</el-radio-button>
                    <el-radio-button label="0.5">50%</el-radio-button>
                    <el-radio-button label="0.75">75%</el-radio-button>
                    <el-radio-button label="1">100%</el-radio-button>
                </el-radio-group>
            </el-form-item>
        </el-form>
        <div class="Purchasefooter">
            <!-- {{$store.state.user.assetList.BTC}} -->
            <p>{{$t('other[2]')}}<span>{{zhongjia}}</span></p>
            <p>{{$t('jiaoyi[6]')}}<span>{{canUseMoney}}</span></p>
        </div>
        <el-button type="success" @click="preservation">{{$t('order[4]')}}</el-button>
        <!-- <el-button type="success" v-if="Prodata.buy_up == 0" >{{$t('order[4]')}}</el-button> -->
    </li>
</template>

<script>
 
export default {
    name: 'marketpricesell',
    props:['Prodata', "codetype","canUseMoney"],
    data () {
        return {
            form:{
                "buy_price": "",//最新价格
                "buy_num": "",//买入数量
                "type": "1",//1买入 2卖出 （必填）
                "otype": "1",//1限价 2市价 （必填）
                "code": "",//产品名称
            },
            radio1: 0,
            code:"",
            zhongjia:"",
        }
    },
    watch: {
        'Prodata.code': function(newVal, oldVal){
            if(newVal){
                this.code = _.toUpper(_.split(newVal, "/")[0]);
                this.form.code = newVal;
                this.form.buy_price = this.Prodata.price;
            };
        },
        // "Prodata.price": function(newVal, oldVal){
        //     if(newVal){
        //         this.form.buy_price = newVal;
        //     };
        // },
        "form.buy_num": function(newVal, oldVal){
            if(newVal){
                this.zhongjia = _.round(_.multiply(_.toFinite(newVal), _.toFinite(this.form.buy_price)), 4)
            }else{
                this.zhongjia = 0;
            }
        },
    },
    methods: {
        buynumchange(data){
            // let balance = this.$store.state.user.assetList.USDT.balance;
            let balance = this.canUseMoney
            let Prodata = _.cloneDeep(this.form);
            
            this.form.buy_num = _.floor(_.multiply(_.divide(_.toFinite(balance), _.toFinite(Prodata.buy_price)), this.radio1), 4);
            // this.zhongjia = _.round(_.multiply(_.divide(_.toFinite(balance), _.toFinite(Prodata.buy_price)), this.radio1), 4);
        },
        preservation(){
            let Prodata = _.cloneDeep(this.form);
            if(!Prodata.buy_price){
                this.$message.error(this.$t('jiaoyi[8]'));
                return false;
            };
            if(!Prodata.buy_num){
                this.$message.error(this.$t('jiaoyi[9]'));
                return false;
            };
            
            this.$emit('func', this.form)
        }
    },
    created(){

    },
    components: {
        
    },
}
</script>

